package com.cskaoyan.service;

import com.cskaoyan.bean.vo.BaseRespVo;
import com.cskaoyan.domain.MarketCategory;
import com.cskaoyan.domain.MarketCategoryExample;
import com.cskaoyan.mapper.MarketCategoryMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class CatalogServiceImpl implements CatalogService {

    @Autowired
    MarketCategoryMapper marketCategoryMapper;


    @Override
    public Map<String, Object> getCurrent(Integer id) {
        Map<String, Object> map = new HashMap<>();


        //currentCategory
        MarketCategory marketCategory = marketCategoryMapper.selectByPrimaryKey(id);
        map.put("currentCategory", marketCategory);

        Integer pid = marketCategory.getId();

        MarketCategoryExample categoryExample = new MarketCategoryExample();
        MarketCategoryExample.Criteria criteria = categoryExample.createCriteria();
        criteria.andPidEqualTo(pid);

        List<MarketCategory> categories = marketCategoryMapper.selectByExampleSelective(categoryExample);
        map.put("currentSubCategory", categories);

        return map;
    }

    @Override
    public BaseRespVo getIndex() {

        MarketCategoryExample categoryExample = new MarketCategoryExample();
        categoryExample.createCriteria().andDeletedEqualTo(false)
                .andPidEqualTo(0)
                .andLevelEqualTo("L1");
        List<MarketCategory> categoryList = marketCategoryMapper.selectByExample(categoryExample);

        MarketCategory currentCategory = categoryList.get(0);

        MarketCategoryExample categoryExample1 = new MarketCategoryExample();
        categoryExample1.createCriteria().andDeletedEqualTo(false)
                .andPidEqualTo(currentCategory.getId());
        List<MarketCategory> currentSubCategory = marketCategoryMapper.selectByExample(categoryExample1);

        Map<String, Object> data = new HashMap<>();

        data.put("categoryList", categoryList);
        data.put("currentCategory", currentCategory);
        data.put("currentSubCategory", currentSubCategory);

        return BaseRespVo.ok(data);
    }

}
